package com.boot.configstatistical.repair;

/**
 * @author Administrator
 * @className Test1
 * @description TODO
 * @date 2023/8/10 16:00
 **/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelColumnUpdater {

    public static void updateColumn(String filePath, int readColumnIndex, int writeColumnIndex) {
        try {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            Sheet sheet = workbook.getSheetAt(0); // Assuming you want to work with the first sheet

            for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
                Row row = sheet.getRow(rowIndex);
                if (row != null) {
                    Cell readCell = row.getCell(readColumnIndex);
                    if (readCell != null) {
                        // Assuming the read column has string values
                        String readValue = readCell.getStringCellValue();

                        // Process the read value and get the corresponding write value
                        String writeValue = processValue(readValue);

                        Cell writeCell = row.createCell(writeColumnIndex);
                        writeCell.setCellValue(writeValue);
                    }
                }
            }

            fileInputStream.close();

            FileOutputStream fileOutputStream = new FileOutputStream(filePath);
            workbook.write(fileOutputStream);
            fileOutputStream.close();

            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static String processValue(String readValue) {
        // Perform any processing needed on the read value to get the write value
        // For this example, let's just append "(Processed)" to the read value
        return readValue + " (Processed)";
    }

    public static void main(String[] args) {
        String filePath = "path/to/your/excel/file.xlsx";
        int readColumnIndex = 0; // Index of the column to read (0-based)
        int writeColumnIndex = 1; // Index of the column to write (0-based)

        updateColumn(filePath, readColumnIndex, writeColumnIndex);

        System.out.println("Column updated successfully!");
    }
}

